home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_203 / gurusguide / audio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  187 lines

  1. /************************************************************************
  2. **********                                                     **********
  3. **********            A U D I O   I N T E R R U P T            **********
  4. **********            -----------------------------            **********
  5. **********                                                     **********
  6. **********        Copyright (C) 1988 Sassenrath Research       **********
  7. **********                All Rights Reserved.                 **********
  8. **********                                                     **********
  9. **********    Example from the "Guru's Guide, Meditation #1"   **********
  10. **********                                                     **********
  11. *************************************************************************
  12. **                                                                     **
  13. **                            - NOTICE -                               **
  14. **                                                                     **
  15. **  The "Guru's Guide, Meditation #1" contains detailed information    **
  16. **  about Amiga interrupts as well as a complete discussion of this    **
  17. **  and other examples.  Meditation #1 and all of its examples were    **
  18. **  written by Carl Sassenrath, the architect of Amiga's multitasking  **
  19. **  operating system.  Copies of the "Guru's Guide" may be obtained    **
  20. **  from:                                                              **
  21. **           GURU'S GUIDE, P.O. BOX 1510, UKIAH, CA 95482              **
  22. **                                                                     **
  23. **  Please include a check for $14.95, plus $1.50 shipping ($4.00 if   **
  24. **  outside North America).  CA residents add 6% sales tax.            **
  25. **                                                                     **
  26. **  This example may be used for any purposes, commercial, personal,   **
  27. **  public, and private, so long as ALL of the above text, copyright,  **
  28. **  mailing address, and this notice are retained in their entirety.   **
  29. **                                                                     **
  30. **  THIS EXAMPLE IS PROVIDED WITHOUT WARRANTY OF ANY KIND.             **
  31. **                                                                     **
  32. ************************************************************************/
  33.  
  34. /*
  35. **  IMPORTANT WARNING:
  36. **  
  37. **  This example steals the AUD0 interrupt vector.  This action
  38. **  defeats the purpose of the Audio Device - but we wanted to
  39. **  keep the example short and to the point.  We advise you to
  40. **  read the documentation for the Audio Device.
  41. */
  42.  
  43. /*
  44. **  COMPILATION NOTE:
  45. **
  46. **  Compiled under MANX AZTEC C 3.6A.  Use the +L compiler option
  47. **  and the "c32" library.  Link with intrsup.o.
  48. */
  49.  
  50.  
  51. #include <exec/exec.h>
  52. #include <hardware/custom.h>
  53. #include <hardware/dmabits.h>
  54. #include <hardware/intbits.h>
  55.  
  56. #define    SIZE 256    /* wave size */
  57.  
  58. struct Interrupt *Intr = NULL;
  59. struct Task *ATask = NULL;
  60. long ASignal = -1;
  61.  
  62. char *WaveForm = NULL;
  63.  
  64. short RepCount;        /* wave rep counter */
  65. short Part;        /* ADSR envelope count */
  66. short Volume;
  67.  
  68. /* Attack, decay, sustain, release tables: */
  69. char ADSR[] = { 3, 3, 5,20 }; /* Reps */
  70. char Ramp[] = {20,-5, 0,-2 }; /* Incr */
  71.  
  72. /* Periods for several "notes" */
  73. short Period[] = { 100,200,300,400,3000,0 };
  74.  
  75.  
  76. /* Interrupt Processing Code */
  77. VOID IntrProc()
  78. {
  79.     int_start();
  80.  
  81.     /* Next ADSR part? */
  82.     if (--RepCount <= 0)
  83.     {
  84.         ++Part;
  85.         RepCount = ADSR[Part];
  86.     }
  87.  
  88.     /* Finished with note? */
  89.     if (Part > 3)
  90.     {
  91.         Volume = 0;
  92.         DisableIntr(INTB_AUD0);
  93.         Signal(ATask,1 << ASignal);
  94.     }
  95.     else /* Change volume & clip */
  96.     {
  97.         Volume += Ramp[Part];
  98.         if (Volume < 0) Volume = 0;
  99.         else if (Volume > 63) Volume = 63;
  100.     }
  101.  
  102.     custom.aud[0].ac_vol = Volume;
  103.     ClearIntr(INTB_AUD0);
  104.  
  105.     int_end();
  106. }
  107.  
  108.  
  109. main()
  110. {
  111.     struct AudChannel *ac;
  112.     int n;
  113.  
  114.     MainInit();
  115.  
  116.     /* Generate unfiltered noise: */
  117.     for (n = 0; n < SIZE; n++)
  118.         WaveForm[n] = (char)rand();
  119.  
  120.     AddHandler(INTB_AUD0, Intr);
  121.  
  122.     /* Turn on audio DMA */
  123.     custom.dmacon = DMAF_SETCLR | DMAF_AUD0 | DMAF_MASTER;
  124.  
  125.     ac = &custom.aud[0];
  126.     ClearIntr(INTB_AUD0);
  127.  
  128.     for (n = 0; Period[n] > 0; n++)
  129.     {
  130.         Volume = 0;
  131.         Part = 0;
  132.         RepCount = ADSR[0];
  133.         ac->ac_per = Period[n];
  134.         ac->ac_ptr = (UWORD *)WaveForm;
  135.         ac->ac_len = SIZE/2;
  136.         ac->ac_vol = Volume;
  137.  
  138.         EnableIntr(INTB_AUD0);
  139.  
  140.         Wait(1 << ASignal);
  141.     }
  142.  
  143.     /* Turn off audio DMA */
  144.     custom.dmacon = DMAF_AUD0;
  145.  
  146.     RemHandler(INTB_AUD0, Intr);
  147.  
  148.     MainExit(0);
  149. }
  150.  
  151.  
  152. MainInit()
  153. {
  154.     extern struct Interrupt *MakeIntr();
  155.     extern long AllocSignal();
  156.     extern struct Task *FindTask();
  157.     extern void *AllocMem();
  158.     extern int Enable_Abort;
  159.  
  160.     Enable_Abort = 0;    /* prevent a CTRL-C */
  161.  
  162.     Intr = MakeIntr("audio.example",0,&IntrProc,0);
  163.     if (Intr == NULL) MainExit(201);
  164.  
  165.     /* Audio DMA needs waveform in chip memory */
  166.     WaveForm = AllocMem(SIZE, MEMF_CHIP | MEMF_CLEAR);
  167.     if (WaveForm == NULL) MainExit(202);
  168.  
  169.     ASignal = AllocSignal(-1);
  170.     if (ASignal == -1) MainExit(203);
  171.  
  172.     ATask = FindTask(NULL);
  173. }
  174.  
  175.  
  176. MainExit(error)
  177.     int error;
  178. {
  179.     FreeIntr(Intr);
  180.  
  181.     if (WaveForm != NULL) FreeMem(WaveForm, SIZE);
  182.  
  183.     if (ASignal != -1) FreeSignal(ASignal);
  184.  
  185.     exit(error);
  186. }
  187.